using System.IO;

namespace Sunny.UI;

public class PCapReader
{
	private string FileName;

	public event OnPCapFrame OnData;

	public PCapReader(string filename)
	{
		FileName = filename;
	}

	public void ReadFile()
	{
		FileStream fileStream = new FileStream(FileName, FileMode.Open);
		BinaryReader binaryReader = new BinaryReader(fileStream);
		binaryReader.ReadBytes(24);
		while (binaryReader.BaseStream.Position < fileStream.Length)
		{
			binaryReader.ReadBytes(8);
			int count = binaryReader.ReadInt32();
			binaryReader.ReadInt32();
			byte[] data = binaryReader.ReadBytes(count);
			this.OnData?.Invoke(this, data, fileStream.Length, binaryReader.BaseStream.Position);
		}
		binaryReader.Close();
		fileStream.Close();
		binaryReader.Dispose();
		fileStream.Dispose();
	}
}
